name: Check internationalization changes

on:
  pull_request:

jobs:
  check_i18n:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Base Setup
        uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1

      - name: Install dependencies
        run: |
          python -m pip install -U pip jupyterlab-translate~=1.0

      - name: Compute hash
        id: currentHash
        run: |
          set -eux
          HASH=$(python scripts/i18n_check.py | tail -n1)
          echo "::set-output name=i18n::${HASH}"

      - name: Checkout target
        run: |
          echo Checking ${GITHUB_BASE_REF}...
          # Ref https://github.com/actions/checkout/issues/93
          git fetch --depth=1 origin +refs/heads/${GITHUB_BASE_REF}:refs/remotes/origin/${GITHUB_BASE_REF}
          git checkout ${GITHUB_BASE_REF}

      - name: Check hash
        env:
          CURRENT: ${{ steps.currentHash.outputs.i18n }}
        run: |
          set -eux
          EXPECTED=$(python scripts/i18n_check.py | tail -n1)
          if [[ "${CURRENT}" != "${EXPECTED}" ]]; then
            echo "Translatable strings have changed, this is only allowed on major or minor version bumps."
            # Set job status as failed if
            # - branch is not "main"
            # - and current version is not a pre release of a zeroed patch version
            if [[ "${GITHUB_BASE_REF}" != "main" && ! ( $(python setup.py --version) =~ ^[0-9]+\.[0-9]+\.0(\.dev|a|b|rc)[0-9]+$ ) ]]; then
              exit 1
            fi
          fi
